import datetime
import requests
import GlobalVariable
import my_influxdb3


def todayPointIncome():
    today_total_point = 0
    today_date = ""
    res = requests.get(GlobalVariable.jd_base_url + "todayPointIncome", headers=GlobalVariable.headers)
    if res.status_code == 200:
        res_json = res.json()
        result = res_json["result"]
        today_total_point = result["todayTotalPoint"]
        todayDate = result["todayDate"]
        today_date = datetime.datetime.strptime(todayDate, "%Y%m%d").strftime("%Y年%m月%d日")
    else:
        errorMessage = res.json()['error']['message']
        print(errorMessage)
        print("Request todayPointIncome failed!")
    GlobalVariable.final_result["today_date"] = today_date
    GlobalVariable.final_result["today_total_point"] = str(today_total_point)
    return today_total_point
 # 今日总积分
def pinTotalAvailPoint():
    total_avail_point = 0
    res = requests.get(GlobalVariable.jd_base_url + "pinTotalAvailPoint", headers=GlobalVariable.headers)
    if res.status_code == 200:
        res_json = res.json()
        result = res_json["result"]
        total_avail_point = result["totalAvailPoint"]
    else:
        print("Request pinTotalAvailPoint failed!")
    GlobalVariable.final_result["total_avail_point"] = str(total_avail_point)
    return total_avail_point

def todayPointDetail():
    params = {
        "sortField": "today_point",
        "sortDirection": "DESC",
        "pageSize": "30",
        "currentPage": "1"
    }
    MACS = []
    res = requests.get(GlobalVariable.jd_base_url + "todayPointDetail", params=params, headers=GlobalVariable.headers)
    if res.status_code == 200:
        res_json = res.json()
        print(res_json)
        result = res_json["result"]
        todayDate = result["todayDate"]
        totalRecord = result["pageInfo"]["totalRecord"]
        pointInfos = result["pointInfos"]
        GlobalVariable.final_result["todayDate"] = datetime.datetime.strptime(todayDate, "%Y%m%d").strftime("%Y-%m-%d")
        GlobalVariable.final_result["totalRecord"] = str(totalRecord)
        GlobalVariable.final_result["pointInfos"] = pointInfos
        # for info in pointInfos:
        #     mac = info["mac"]
        #     MACS.append(mac)
        #     routerActivityInfo(mac)
        #     routerAccountInfo(mac)
        #     pointOperateRecordsShow(mac)
        #
        # JDServiceAPI.getListAllUserDevices()
        #
        # for mac in MACS:
        #     JDServiceAPI.getControlDevice(mac, 2)
        #     JDServiceAPI.getControlDevice(mac, 3)
    else:
        errorMessage = res.json()['error']['message']
        print(errorMessage)
        print("Request todayPointDetail failed!")
# 主操作
def main():

    if GlobalVariable.RECORDSNUM.isdigit():
        GlobalVariable.records_num = int(GlobalVariable.RECORDSNUM)
    todayPointDetail()
    final_result = GlobalVariable.final_result["pointInfos"]
    for x in final_result:
        # 当天积分
        today_total_point=x["todayPointIncome"]
        # 今日总积分
        totalAvailPoint=x["availableAmount"]
        mac=x["mac"]


        info_list=[]
    # #
        info_list.append({"today_total_point":today_total_point})
        info_list.append({"totalAvailPoint":totalAvailPoint})
        print(info_list)
        client = my_influxdb3.creat_client()
    # # 传一个tag  多个file时要传一个集合 不支持中文
        my_influxdb3.add(client, "JD", mac, info_list)




# endregion

def runTest(i):
    # if i > 10:
    #     return
    # try:
    if GlobalVariable.WSKEY is None or GlobalVariable.WSKEY.strip() == '':
        print("未获取到环境变量'WSKEY'，执行中止")
        return
    GlobalVariable.headers["wskey"] = GlobalVariable.WSKEY
    GlobalVariable.service_headers["tgt"] = GlobalVariable.WSKEY
    main()
    # except Exception as e:
    #     print("出现错误：", e)
    #     print("准备重新执行...")
    #     time.sleep(3)
    #     runTest(++i)


# 读取配置文件
if __name__ == '__main__':
    runTest(0)

